<?php

class Truck_appointment extends CActiveRecord
{
	/**
	 * The followings are the available columns in table 'Truck_appointment':
	 * @var integer $id
	 * @var integer $fk_truck_id
	 * @var integer $fk_board_id
	 * @var integer $fk_driver1_id
	 * @var integer $fk_driver2_id
	 * @var integer $loading_status
	 * @var double $mileage
	 * @var string $amount_kind
	 * @var double $advance_payment
	 * @var double $balance
	 * @var double $total_amount
	 * @var string $remark
	 * @var double $gas_fee
	 * @var double $bridge_charge
	 * @var string $origin
	 * @var string $destination
	 * @var string $fuel_name
	 * @var double $extra_cost
	 * @var double $total_cost
	 * @var integer $state
	 * @var string $create_time
	 */
	
	//查询用临时变量
	public $consignor_name;
	public $consignee_name;
	public $goods_name;
	public $goods_demand_deliver_time;
	//查询用临时变量
	
	//派车单导入确认单临时变量
	public $front_fuel;
	public $empty_fuel;
	public $light_fuel;
	public $heavy_fuel;
	public $compute_ratio;
	//派车单导入确认单临时变量
	
	public $truck_plate_no;
	public $board_plate_no;
	public $driver1_name;
	public $driver2_name;
	public $driver1_mobile;
	public $driver2_mobile;
	public $type;
	public $truck_type;//车头的车型
	public $truck_type_id;//车头车型id
	public $truck_type_ratio;//派车单导入确认单 车头车型绩效比率
	public $create_user;
	public $update_user;
	public $send_sms=1;
	private $_loading_status=array('0'=>'重驶','1'=>'指定空驶');
	//private $_fuel_name=array('plain_empty'=>'平原车头空驶油耗','ghat_empty'=>'山路车头空驶油耗','plain_min'=>'平原空驶或载货10吨以下油耗',
	//'ghat_min'=>'山路空驶或载货10吨以下油耗','plain_mid'=>'平原轻驶油耗','ghat_mid'=>'山路轻驶油耗','plain_max'=>'平原重驶油耗','ghat_max'=>'山路重驶油耗');
	private $_amount_kind=array('0'=>'现金','1'=>'转账','2'=>'其他');
	private $_send_sms=array('0'=>'是','1'=>'否');

	/**
	 * Returns the static model of the specified AR class.
	 * @return CActiveRecord the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'truck_appointment';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		return array(
			array('remark','length','max'=>200),
			array('prepaid_place','length','max'=>50),
			array('loading_status', 'numerical', 'integerOnly'=>true),
			array('mileage,driver_prepaid', 'numerical'),
			array('truck_plate_no,driver1_name,driver1_mobile,type,mileage,driving_circuit,send_sms,business_time,driver_prepaid', 'required'),
			array('mileage', 'match','pattern' => '/^\d{1,8}(\.\d{1,2})?$/','message'=>'{attribute} 整数位最多8位，小数位2位'),
			array('driver_prepaid', 'match','pattern' => '/^-?\d{1,16}(\.\d{1,2})?$/','message'=>'{attribute} 整数位最多16位，小数位2位'),
			array('business_time', 'type','type'=>'date','message'=>'{attribute} 必须为日期型(年-月-日)'),
			
			//重驶与制定空驶验证 生成客户端验证
			array('driving_circuit', 'numerical', 'integerOnly'=>true,'on'=>'all'),
			array('special_reasons','length','max'=>100,'on'=>'all'),
			array('advance_payment,balance,total_amount', 'numerical','on'=>'all'),
			array('amount_kind,advance_payment,balance,total_amount', 'required','on'=>'all'),
			array('advance_payment,balance,total_amount', 'match','pattern' => '/^-?\d{1,16}(\.\d{1,2})?$/','message'=>'{attribute} 整数位最多16位，小数位2位','on'=>'all'),
			array('origin','length','max'=>50,'on'=>'all'),
			array('destination','length','max'=>50,'on'=>'all'),
			array('fuel_name','length','max'=>20,'on'=>'all'),
			array('gas_fee,gas_amount,bridge_charge, extra_cost, total_cost', 'numerical','on'=>'all'),
			array('gas_fee,gas_amount,bridge_charge,origin,destination,fuel_name,extra_cost,total_cost', 'required','on'=>'all'),
			array('gas_fee,bridge_charge, extra_cost, total_cost', 'match','pattern' => '/^-?\d{1,16}(\.\d{1,2})?$/','message'=>'{attribute} 整数位最多16位，小数位2位','on'=>'all'),
			array('gas_amount', 'match','pattern' => '/^-?\d{1,8}(\.\d{1,2})?$/','message'=>'{attribute} 整数位最多8位，小数位2位','on'=>'all'),
			
			//重驶验证
			array('special_reasons','length','max'=>100,'on'=>'heaven'),
			array('advance_payment,balance,total_amount', 'numerical','on'=>'heaven'),
			array('amount_kind,advance_payment,balance,total_amount', 'required','on'=>'heaven'),
			array('advance_payment,balance,total_amount', 'match','pattern' => '/^-?\d{1,16}(\.\d{1,2})?$/','message'=>'{attribute} 整数位最多16位，小数位2位','on'=>'heaven'),
			
			//指定空驶验证
			array('driving_circuit', 'numerical', 'integerOnly'=>true,'on'=>'empty'),
			array('origin','length','max'=>50,'on'=>'empty'),
			array('destination','length','max'=>50,'on'=>'empty'),
			array('fuel_name','length','max'=>20,'on'=>'empty'),
			array('gas_fee,gas_amount, bridge_charge, extra_cost, total_cost', 'numerical','on'=>'empty'),
			array('gas_fee,gas_amount,bridge_charge,origin,destination,fuel_name,extra_cost,total_cost', 'required','on'=>'empty'),
			array('gas_fee, bridge_charge, extra_cost, total_cost', 'match','pattern' => '/^-?\d{1,16}(\.\d{1,2})?$/','message'=>'{attribute} 整数位最多16位，小数位2位','on'=>'empty'),
			array('gas_amount', 'match','pattern' => '/^-?\d{1,8}(\.\d{1,2})?$/','message'=>'{attribute} 整数位最多8位，小数位2位','on'=>'empty'),
		);
	}
	
	/**
	 * Prepares attributes before performing validation.
	 */
	protected function beforeValidate($on)
	{
		if($this->isNewRecord){
			$this->state=0;
			$this->create_time=date('Y-m-d G:i:s');
			$this->fk_create_user_id=Yii::app()->session['id'];
		}
		$this->update_time=date('Y-m-d G:i:s');
		$this->fk_update_user_id=Yii::app()->session['id'];
		return true;
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			//查询用临时变量
			'consignor_name'=>'货主(发货人)1',
			'consignee_name'=>'客户(收货人)1',
			'goods_name'=>'货物名称1',
			'goods_demand_deliver_time'=>'提货时间',
			//查询用临时变量
			'id' => '索引号',
			'appointment_no' => '派车单号',
			'fk_truck_id' => '车辆id',
			'fk_board_id' => '尾板id',
			'fk_driver1_id' => '司机1 id',
			'fk_driver2_id' => '司机2 id',
			'truck_plate_no' =>'车牌号',
			'board_plate_no' =>'尾板牌号',
			'type' => '车型',
			'driver1_name' =>'司机1姓名',
			'driver2_name' =>'司机2姓名',
			'driver1_mobile' =>'司机1手机',
			'driver2_mobile' =>'司机2手机',
			'loading_status' => '行驶状态',//0重驶 1指定空驶
			'mileage' => '运输总里程',
			'driving_circuit' => '行驶线路', //0高速 1国道 2辅路
			'amount_kind' => '收入类型',
			'special_reasons' => '特殊原因',
			'advance_payment' => '预付款',
			'balance' => '尾款',
			'total_amount' => '收入总额',
			'remark' => '备注',
		    //指定空驶 start
		    'gas_amount' =>'油量(升)',
			'gas_fee' => '油费',
			'bridge_charge' => '路桥费',
			'origin' => '始发地',
			'destination' => '目的地',
			'fuel_name' => '标准油耗', //对应标准油耗表内8种状况之一 plain_empty、ghat_empty、plain_min.....
			'extra_cost' => '特殊费用',
			'total_cost' => '费用总额',
			//指定空驶 end
			'state' => '状态',
			'driver_prepaid' => '司机预支费用',
			'prepaid_place' => '预支地点',
			'create_user' => '录入人',
			'update_user' => '修改人',
			'update_time' => '最后修改时间',
			'create_time' => '创建时间',
			'send_sms'=>'发送短信',
			'business_time'=>'业务时间'
		);
	}
	
	public function excelAttributes(){//导出excel所需的字段名、列号、宽度
		return array(
			'appointment_no'=>array('B',12),
			'loading_status'=>array('C',12,'getLoadingStatusName'),
			'truck_plate_no'=>array('D',12),
			'board_plate_no'=>array('E',12,'format'=>'0'),
			'type'=>array('F',12),
			'driver1_name'=>array('G',12),
			'consignor_name'=>array('H',12),
			'consignee_name'=>array('I',12),
			'goods_name'=>array('J',12),
			'origin'=>array('K',12),
			'destination'=>array('L',12),
			'goods_demand_deliver_time'=>array('M',12,'getGoodsDemandDeliverTime'),
			'business_time'=>array('N',12),
			'create_time'=>array('O',20),
		);
	}
	
	public function getGoodsDemandDeliverTime(){
		return $this->goods_demand_deliver_time!=''?substr($this->goods_demand_deliver_time,0,10):'';
	}
	
	public function getLoadingStatusNames(){
		return $this->_loading_status;
	} 
	
	public function getLoadingStatusName($n=null){
		return $this->_loading_status[$n==null?$this->loading_status:$n];
	}
	
	public function getDrivingCircuitNames(){
		return Yii::app()->params['driving_circuit'];
	} 
	
	public function getDrivingCircuitName($n=null){
		return  Yii::app()->params['driving_circuit'][$n==null?$this->driving_circuit:$n];
	}
	
	public function getFuelNames(){
		return Yii::app()->params['fuel_consumption'];
	}
	
	public function getFuelName($n=null){
		return Yii::app()->params['fuel_consumption'][$n==null?$this->fuel_name:$n];
	}
	
	public function getAmountKindNames(){
		return $this->_amount_kind;
	}
	
	public function getAmountKindName($n=null){
		return $this->_amount_kind[$n==null?$this->amount_kind:$n];
	}
	
	public function getSendSmsNames(){
		return $this->_send_sms;
	}
	
	public function safeAttributes(){
		$params=parent::safeAttributes();
		$params[]='board_plate_no';
		$params[]='driver1_name';
		$params[]='driver2_name';
		$params[]='driver1_mobile';
		$params[]='driver2_mobile';
		$params[]='type';
		$params[]='truck_plate_no';
		$params[]='send_sms';
		return $params;
	}
}